//
//  MOperatePwdViewController.m
//  NeusoftHBSJSober
//
//  Created by Apple-21 on 15/11/7.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "MOperatePwdViewController.h"
#import "CheckMobile.h"
#import "MVIPCardService.h"
#import "MDDataBaseUtil.h"
#import "MViewUtil.h"
#import "MResetPwdViewController.h"
@interface MOperatePwdViewController ()<UITextFieldDelegate> {

    CheckMobile *checkMobile;
    
    MViewUtil *viewUtil;
    
    MResetPwdViewController *resetPwdVC;
}
- (IBAction)backAction:(id)sender;
@property (weak, nonatomic) IBOutlet UIView *contentView;
- (IBAction)okBtnAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *okBtn;
@property (weak, nonatomic) IBOutlet UIButton *resetPwdBtn;
- (IBAction)resetPwdAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITextField *pwdTxt;
@property (weak, nonatomic) IBOutlet UITextField *confirmPwd;
@property (weak, nonatomic) IBOutlet UITextField *phoneTxt;

@end

@implementation MOperatePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self _initView];
}
- (void)_initView {

    [self.okBtn.layer setCornerRadius:3.0];
    [self.okBtn.layer setMasksToBounds:YES];
    [self.okBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.okBtn.layer setBorderWidth:1.0];
    
    [self.resetPwdBtn.layer setCornerRadius:3.0];
    [self.resetPwdBtn.layer setMasksToBounds:YES];
    [self.resetPwdBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.resetPwdBtn.layer setBorderWidth:1.0];
    
    [self.contentView.layer setCornerRadius:5.0];
    [self.contentView.layer setMasksToBounds:YES];
    
    [self.bottomView.layer setCornerRadius:5.0];
    [self.bottomView.layer setMasksToBounds:YES];
    self.phoneTxt.delegate = self;
    self.pwdTxt.delegate = self;
    self.confirmPwd.delegate = self;
    
    checkMobile = [[CheckMobile alloc]init];
    
    viewUtil = [[MViewUtil alloc]init];
    
    //13106585373
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [self.pwdTxt resignFirstResponder];
    [self.confirmPwd resignFirstResponder];
    [self.phoneTxt resignFirstResponder];
}
- (IBAction)backAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)setAlertViewWithTitle:(NSString *)title {

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:title message:nil delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
    [alert show];
}
- (IBAction)okBtnAction:(UIButton *)sender {
    
    if ([self.pwdTxt.text isEqualToString:@""]) {
        
        [self setAlertViewWithTitle:@"请输入操作密码"];
    } else {
    
        if ([self.confirmPwd.text isEqualToString:@""]) {
            [self setAlertViewWithTitle:@"请再次输入操作密码"];
        }else {
        
            if ([self.phoneTxt.text isEqualToString:@""]) {
                [self setAlertViewWithTitle:@"请输入手机号码"];
            } else {
            
                if (![self.confirmPwd.text isEqualToString:self.pwdTxt.text]) {
                    [self setAlertViewWithTitle:@"您输入的密码不匹配"];
                } else {
                    
                    if ([checkMobile validateMobile:self.phoneTxt.text]) {
                        
                        [viewUtil showLoadingText:nil targetView:self.view];
                        [[MVIPCardService sharedClient]getVIPCardPassWordWithType:@"1" userId:[[MDDataBaseUtil userID]integerValue] password:self.pwdTxt.text phoneNumber:self.phoneTxt.text completion:^(NSString *result){
                        
                            NSLog(@"result %@",result);
                            [viewUtil hideLoadingTextFromTargetView:self.view];
                            [self dismissViewControllerAnimated:YES completion:nil];
                            
                        } failure:^(NSString *error){
                        
                            [viewUtil showShortDurationMessage:error targetView:self.view];
                            NSLog(@"error %@",error);
                        }];
                        
                        
                    } else {
                    
                        [self setAlertViewWithTitle:@"您输入的手机格式错误"];
                    }
                    
                }
                
            }
        }
    }
}
- (IBAction)resetPwdAction:(UIButton *)sender {
    
    resetPwdVC = [[MResetPwdViewController alloc]init];
    resetPwdVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:resetPwdVC animated:YES completion:nil];
}
@end
